﻿package 
{
	import flash.display.Bitmap;
	import flash.events.Event;
	
	import org.papervision3d.core.geom.renderables.Triangle3D;
	import org.papervision3d.core.geom.renderables.Vertex3D;
	import org.papervision3d.core.geom.TriangleMesh3D;
	import org.papervision3d.core.math.NumberUV;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.view.BasicView;
	
	import net.hires.debug.Stats;
	
	/**
	 * ...
	 * @author Edankwan@amiciforce
	 */
	
	public class Main extends BasicView 
	{
		[Embed(source = '../bin/heightmap.png')] private var HeightMapClass: Class;
		[Embed(source = '../bin/texture.png')] private var TextureClass: Class;
		
		private var mesh: TriangleMesh3D;
		private var bitmapHeightMap: Bitmap = new HeightMapClass();
		private var texture: Bitmap = new TextureClass();
		private var material: BitmapMaterial;
		private const maxHeight: Number = 15;
		private const meshScale: Number = 15;
		
		public function Main():void 
		{
			stage.quality = "low";
			camera.focus = 10;
			camera.zoom = 100;
			camera.y = 1000;
			material = new BitmapMaterial(texture.bitmapData);
			mesh = new TriangleMesh3D(material, [], []);
			
			var vertices :Array  = mesh.geometry.vertices;
			var faces :Array  = mesh.geometry.faces;
			var i:int;
			var j:int;
			
			for (i = 0; i < bitmapHeightMap.width; i++) {
				for (j = 0; j < bitmapHeightMap.height; j++) {
					var x: Number = i-bitmapHeightMap.width / 2;
					var y: Number = j-bitmapHeightMap.height / 2;
					var z: Number = -(bitmapHeightMap.bitmapData.getPixel(i, bitmapHeightMap.height-j) & 0xFF) / 255 * maxHeight;
					vertices.push(new Vertex3D(x, y, z));
				}
			}
			for (i = 0; i < bitmapHeightMap.width - 1; i++) {
				for (j = 0; j < bitmapHeightMap.height - 1; j++) {
					
					/*
					 * Triangle 1
					 * 1---2
					 * | /
					 * 3
					 * 
					 */
					var vertex1: Vertex3D = vertices[i * bitmapHeightMap.height + j];
					var vertex2: Vertex3D = vertices[(i + 1) * bitmapHeightMap.height + j];
					var vertex3: Vertex3D = vertices[i * bitmapHeightMap.height + j + 1];
					var uv1: NumberUV = new NumberUV(i / bitmapHeightMap.width, j / bitmapHeightMap.height);
					var uv2: NumberUV = new NumberUV((i + 1) / bitmapHeightMap.width, j / bitmapHeightMap.height);
					var uv3: NumberUV = new NumberUV(i / bitmapHeightMap.width, (j + 1) / bitmapHeightMap.height);
					
					faces.push(new Triangle3D(mesh, [vertex1, vertex2, vertex3], null, [uv1, uv2, uv3]));
					
					/* 
					 * Triangle 2
					 *     2
					 *   / |
					 * 1---3
					 * 
					 */
					vertex1 = vertices[i * bitmapHeightMap.height + j + 1];
					vertex2 = vertices[(i + 1) * bitmapHeightMap.height + j];
					vertex3 = vertices[(i + 1) * bitmapHeightMap.height + j+1];
					uv1 = new NumberUV(i / bitmapHeightMap.width, (j + 1) / bitmapHeightMap.height);
					uv2 = new NumberUV((i + 1) / bitmapHeightMap.width, j / bitmapHeightMap.height);
					uv3 = new NumberUV((i + 1) / bitmapHeightMap.width, (j + 1) / bitmapHeightMap.height);
					
					faces.push(new Triangle3D(mesh, [vertex1, vertex2, vertex3], null, [uv1, uv2, uv3]));
				}
			}
			mesh.scale = meshScale;
			scene.addChild(mesh);
			camera.lookAt(mesh);
			mesh.rotationX = 90;
			startRendering();
			
			addChild(new Stats());
		}
		
		override protected function onRenderTick(event:Event = null):void
		{
			mesh.rotationY++;
			super.onRenderTick(event);
		}

	}
	
}